package com.boomsecret.broadcastreceiverdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.boomsecret.receiver.NetWorkChangeBroadcastReceiver;

public class MainActivity extends Activity {

    private static String Tag = MainActivity.class.getSimpleName();

    private NetWorkChangeBroadcastReceiver receiver = null;

    private IntentFilter intentFilter = null;

    private Button button = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myRegisterReceiver();
        initViews();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.boomsecret.demo.DEMO");
                sendBroadcast(intent);
//                sendOrderedBroadcast(intent, null);
            }
        });
    }

    private void initViews() {
        button = (Button) findViewById(R.id.btn);
    }

    /**
     * 动态注册广播
     */
    private void myRegisterReceiver() {
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        receiver = new NetWorkChangeBroadcastReceiver();
        registerReceiver(receiver, intentFilter);
    }

    /**
     * 动态解绑已注册广播
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
